package com.numbfish.sample;

import com.nimbusds.jose.*;
import com.nimbusds.jose.crypto.MACSigner;
import org.junit.jupiter.api.Test;

import java.security.SecureRandom;

public class Ex01_JWS_Helloword {
    @Test
    public void test1() throws Exception {
        // Create an HMAC-protected JWS object with a string payload
        JWSObject jwsObject = new JWSObject(new JWSHeader(JWSAlgorithm.HS256),
                new Payload("Hello,world!"));

// We need a 256-bit key for HS256 which must be pre-shared
        byte[] sharedKey = new byte[32];
        new SecureRandom().nextBytes(sharedKey);

// Apply the HMAC to the JWS object
        jwsObject.sign(new MACSigner(sharedKey));

// Output in URL-safe format
        System.out.println(jwsObject.serialize());

        System.out.println(jwsObject.getHeader().getAlgorithm());
        System.out.println(jwsObject.getPayload());
    }

}

